/*
 * Copyright (C) 2011 Patrik 乲erfeldt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.com.guanying.android.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Gallery;

/**
 * A horizontally scrollable {@link ViewGroup} with items populated from an
 * {@link Adapter}. The ViewFlow uses a buffer to store loaded {@link View}s in.
 * The default size of the buffer is 3 elements on both sides of the currently
 * visible {@link View}, making up a total buffer size of 3 * 2 + 1 = 7. The
 * buffer size can be changed using the {@code sidebuffer} xml attribute.
 * 
 */
public class ViewFlow extends Gallery {

	 public ViewFlow(Context paramContext)
	  {
	    super(paramContext);
	  }

	  public ViewFlow(Context paramContext, AttributeSet paramAttributeSet)
	  {
	    super(paramContext, paramAttributeSet);
	  }

	  public ViewFlow(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
	  {
	    super(paramContext, paramAttributeSet, paramInt);
	  }

	  private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {   
		   return e2.getX() > e1.getX();   
		  }  

	  @Override  
	  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,   
	    float velocityY) {   
	   int kEvent;   
	   if (isScrollingLeft(e1, e2)) {   
	    // Check if scrolling left   
	    kEvent = KeyEvent.KEYCODE_DPAD_LEFT;   
	   } else {   
	    // Otherwise scrolling right   
	    kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;   
	   }   
	   onKeyDown(kEvent, null);   
	   return true;   
	  }  


}
